<!DOCTYPE html>
<html>
<head>
    <title>ads form</title>
    <#include "/header.html">
    <link rel="stylesheet" href="${request.contextPath}/statics/js/lay-module/dtree/dtree.css" media="all">
    <link rel="stylesheet" href="${request.contextPath}/statics/js/lay-module/dtree/font/dtreefont.css" media="all">
    <style>
        .layui-upload-img {
            max-height: 100px;
        }
    </style>
</head>
<body class="bg-white">

<div id="appMain">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" name="type" value="1" title="目录" lay-filter="menuType" checked="">
                <input type="radio" name="type" value="2" title="文章" lay-filter="menuType">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上级</label>
            <div class="layui-input-block">
                <div id="menuTree" class="dtree" data-id="0" ></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="输入标题" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">内容</label>
            <div class="layui-input-block">
            <textarea rows="" name="content" cols="" lay-verify="required" lay-reqtext="标题不能为空" placeholder="输入内容" class="layui-textarea"></textarea>
               
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <input type="hidden" name="img" value="">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-sm layui-bg-black" id="uploadBtn">选择图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="uploadPreview" src="">
                        <p id="uploadText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="0" title="禁用">
                <input type="radio" name="status" value="1" title="启用" checked="">
            </div>
        </div>
        <!-- <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" name="sort" placeholder="数值越大排越前" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" name="remark" placeholder="输入备注信息" value="" class="layui-input" autocomplete="off">
            </div>
        </div> -->
        <!-- <div class="layui-form-item layui-form-text editor-item">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block" id="editor">
            </div>
        </div> -->

        <div class="layui-hide">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    layui.use(['form', 'dtree', 'upload', 'wangEditor'], function () {
        var form = layui.form,
            dtree = layui.dtree,
            upload = layui.upload,
            wangEditor = layui.wangEditor,
            $ = layui.$;

        var editor = new wangEditor('#editor');
        editor.customConfig.showFullScreen = true;
        editor.customConfig.uploadImgServer = serverUrl('oss/file/upload');
        editor.customConfig.uploadFileName = 'image';
        editor.customConfig.pasteFilterStyle = false;
        editor.customConfig.uploadImgMaxLength = 1;
        editor.customConfig.uploadImgHooks = {
            timeout: function (xhr, editor) {
                layer.msg('上传超时！')
            },
            // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
            customInsert: function (insertImg, result, editor) {
                console.log(result);
                if (result.code === 0) {
                    insertImg(result.url);
                } else {
                    layer.msg(result.msg);
                }
            }
        };
        editor.customConfig.customAlert = function (info) {
            layer.msg(info);
        };

        var _parent = parent;
        dtree.render({
            elem: "#menuTree",
            data: _parent.menuTreeData,
            icon: "-1",
            selectInputName: {
                nodeId: "parentId",
                context: "parentName"
            },
            selectTips: "无",
            // initLevel: 3,
            select: true, //指定下拉树模式
            done: function(res, $ul, first){
                if(first && _parent.formDataId > 0) {
                    $.ajax({
                        type: "POST",
                        url: serverUrl('article/info/' + _parent.formDataId),
                        success: function (result) {
                            if (result.code === 0) {
                                form.val('dataForm', result.info);
                                $("#editor").html(result.info.content);
                                dtree.dataInit("menuTree", result.info.parentId);
                                dtree.selectVal("menuTree");
                                typeTplUpdate(result.info.type);
                                $('#uploadPreview').attr('src', result.info.img); //url
                                //form.render();
                                editor.create();
                            }
                        }
                    });
                } else {
                    typeTplUpdate(1);
                    editor.create();
                }
            }
        });

        //不同类型菜单显示
        var typeTplUpdate = function(type) {
            $(".layui-form-item").removeClass('layui-hide');
            if (type == 1) {
                $(".editor-item").addClass('layui-hide');
            }
        }
        form.on('radio(menuType)', function(data){
            typeTplUpdate(data.value);
        });

        var submitForm = function () {
            var params = form.val('dataForm');
            params['id'] = _parent.formDataId || null;
            //params['content'] = editor.txt.html();
            if (params['parentId'] == '') {
                params['parentId']= 0;
            }
            var method = _parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('article/'+method, params, function (res) {
                _parent.table.reload();
                _parent.loadMenuData();
            });
        };

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadBtn'
            ,url: serverUrl('oss/file/upload')
            ,auto: false
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#uploadPreview').attr('src', result); //base64
                });
            }
            ,done: function(res){
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                form.val('dataForm', {'img': res.url});
                submitForm();
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            /*var params = data.field;
            params['content'] = editor.txt.html();
            alert(JSON.stringify(params));
            return false;*/
            var img = $('#uploadPreview').attr('src');
            if (img.indexOf('data:') === 0) {
                uploadInst.upload();
                return false;
            }
            submitForm();
            return false;
        });

    });
</script>
</body>
</html>